﻿#region Imports
using System;
using System.Web.UI.WebControls;
using Spring.Data.NHibernate.Support;
using d3PrtDgn.Common.Common;
using d3PrtDgn.Data.IDAO;
#endregion

namespace d3PrtDgn.Web.Admin
{
	public partial class Users : Page
	{
		#region Private fields
		private IUserDao userDao;
        private Data.Domain.Entity.User selectedUser;

		#endregion

		#region Public properties
		/// <summary>
		/// User DAO
		/// </summary>
		public IUserDao UserDao
		{
			set { userDao = value; }
		}

		/// <summary>
		/// Gets currently selected user for editing
		/// </summary>
        public Data.Domain.Entity.User SelectedUser
		{
			get { return selectedUser; }
		}
		#endregion

		#region Event Handlers
		protected override void OnInit(EventArgs e)
		{
			if (SignedInUser.Id == 0 || SignedInUser.Username != "admin")
				SetResult("Login");

			base.OnInit(e);
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				initGrid();
			}
		}

		/// <summary>
		/// Changes index of selected page
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void PageIndexChanging(object sender, GridViewPageEventArgs e)
		{
			gvUsers.PageIndex = e.NewPageIndex;
			initGrid();
		}

		/// <summary>
		/// Deletes user
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void DeleteUser(object sender, GridViewDeleteEventArgs e)
		{
			using(new SessionScope())
			{
                Data.Domain.Entity.User usr = userDao.FindById((int)gvUsers.DataKeys[e.RowIndex].Value);
				userDao.Delete(usr); 
			}
			SetResult("Users");
		}

		/// <summary>
		/// Saves selected user
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void SaveUser(object sender, EventArgs e)
		{
			using (new SessionScope())
			{
				userDao.SaveOrUpdate(selectedUser);
			}
			SetResult("Users");
		}

		/// <summary>
		/// Sets user for editing according to selected user in grid
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void EditUser(object sender, GridViewSelectEventArgs e)
		{
			selectedUser = userDao.FindById((int)gvUsers.DataKeys[e.NewSelectedIndex].Value);

		}

		/// <summary>
		/// Cancels editing
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void Cancel(object sender, EventArgs e)
		{
			SetResult("Users");
		}
		#endregion

		#region Model Management and Data Binding Methods
		protected override void InitializeModel()
		{
            selectedUser = new Data.Domain.Entity.User();
		}

		protected override void LoadModel(object savedModel)
		{
            selectedUser = (Data.Domain.Entity.User)savedModel;
		}

		protected override object SaveModel()
		{
			return selectedUser;
		}

		protected override void InitializeDataBindings()
		{
			BindingManager.AddBinding("tbName.Text", "SelectedUser.Name");
			BindingManager.AddBinding("tbSurname.Text", "SelectedUser.Surname");
			BindingManager.AddBinding("tbEmail.Text", "SelectedUser.Email");
			BindingManager.AddBinding("tbUsername.Text", "SelectedUser.Username");
			BindingManager.AddBinding("tbPassword.Text", "SelectedUser.Password");
		}
		#endregion

		#region Private methods

		/// <summary>
		/// Initializes grid
		/// </summary>
		private void initGrid()
		{
			gvUsers.DataSource = userDao.FindAll();
			gvUsers.DataBind();
		}
		#endregion
	}
}
